package org.dna.wickwtui.activities;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.dna.UserRoles;
import org.dna.uimodel.MenuItem;
import org.dna.uimodel.SelectableActivity;
import org.dna.wickwtui.MetroPage;
import org.tigris.scarab.model.Activity;

/**
 * Page used to create a new or modify an existing activity.
 * */
public class ManageActivityPage extends MetroPage {

    private static final Logger LOG = Logger.getLogger(ManageActivityPage.class);

    @UserRoles({"admin", "project_maneger"})
    private class NewActivityLink extends Link<Void> {

        private static final long serialVersionUID = 3694821522563896705L;

        public NewActivityLink(String id) {
            super(id);
        }

        @Override
        public void onClick() {
            switchToPanel(ActivityDetailsPanel.class);
        }

        @Override
        public boolean isEnabled() {
            return m_currentPanel instanceof ActivityListPanel;
        }
    }

    @UserRoles({"admin", "user"})
    private class ListActivityLink extends Link<Void> {

        private static final long serialVersionUID = 4258377978719477979L;

        public ListActivityLink(String id) {
            super(id);
        }

        @Override
        public void onClick() {
            switchToPanel(ActivityListPanel.class);
        }

        @Override
        public boolean isEnabled() {
            return m_currentPanel instanceof ActivityDetailsPanel;
        }
    }

    /**
     * Boorkmarkable constructor
     * */
    public ManageActivityPage() {
        configurePanels();
        m_currentPanel = m_panelsMap.get(ActivityDetailsPanel.class);
        add(m_currentPanel);
        m_pageContextMenu = preparePageContextMenu();
        updateContextMenu();
    }

    /**
     * Sets the current activity to be displayed by this page, force the showing
     * of ActivityDetailsPanel with the given activity.
     * */
    public void setCurrentActivity(Activity act) {
        switchToPanel(ActivityDetailsPanel.class, act);
    }

    private void configurePanels() {
        IModel<List<SelectableActivity>> allActivitiesModel = new LoadableDetachableModel<List<SelectableActivity>>() {

            @Override
            protected List<SelectableActivity> load() {
                return allActivities();
            }
        };
        ActivityDetailsPanel activityDetailsPanel = new ActivityDetailsPanel("innerPanel");
        ActivityListPanel userListPanel = new ActivityListPanel("innerPanel",
                allActivitiesModel);

        m_panelsMap.put(ActivityDetailsPanel.class, activityDetailsPanel);
        m_panelsMap.put(ActivityListPanel.class, userListPanel);
    }

    private List<MenuItem> preparePageContextMenu() {
        MenuItem newActivityItem = new MenuItem("New activity", ManageActivityPage.class) {

            @Override
            public Component createMenu() {
                NewActivityLink link = new NewActivityLink("link");
                link.add(new Label("caption", getCaption()));
                return link;
            }
        };

        MenuItem allUsersItem = new MenuItem("All activities", ManageActivityPage.class) {

            @Override
            public Component createMenu() {
                ListActivityLink link = new ListActivityLink("link");
                link.add(new Label("caption", getCaption()));
                return link;
            }
        };
        return Arrays.asList(newActivityItem, allUsersItem);
    }
}
